import pathToRegexp from 'path-to-regexp';
import { evaluates, addCartOrBuy,evaluateLike } from './api';
import { Toast } from 'antd-mobile';
import { routerRedux } from 'dva/router';

export default {
    namespace: 'evaluateList',
    state: {
        list:[],
        evaluteEntity:{},
        hasMore:true,
        specId:"",
        goodsId:"",
        gevalScore:null,
        pageNo:1,
        gevalImg:'',
        pageSize:""
    },
    reducers: {
        save(state, { data }) {
            return { ...state, ...data };
        },

        saveList(state, { data }) {

            if( data.pageNo !==undefined && data.pageNo == 1 ){

                return { ...state, ...data, hasMore: data.list.length == data.pageSize };
              //  yield put({ type: 'save', data: { ...result.ret, ...obj, hasMore: result.ret.list.length == result.pageSize } });
            }else {
                return { ...state, ...data, hasMore: data.list.length == data.pageSize, list:[...state.list, data.list ]  };
            }

        },


    },
    effects: {
        *evaluatesEFF({ obj }, { put, call, select }) {
            //Toast.loading();
           // console.log(obj)
            const evaluateList = yield select((state)=>state.evaluateList);
            const result = yield call(evaluates, { specId:evaluateList.specId, goodsId:evaluateList.goodsId, gevalScore:evaluateList.gevalScore, pageNo:evaluateList.pageNo,gevalImg:evaluateList.gevalImg, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result  });
                // if(result.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, list: result.ret.list,pageSize:result.pageSize,pageNo:result.pageNo} });
                if(result.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, list: result.ret.list} });

                else yield put({ type: 'save', data: { ...result.ret,list: [...evaluateList.list, ...result.ret.list] } });

                return { r:true };

            } else {

            }
        },

        *dianzanEFF({ obj }, { put, call }) {

            const result = yield call(evaluateLike, obj);
            if (result.code == 200) {
                Toast.success(result.msg,1.5)
            } else {

            }
            return result
        },


        *addCartOrBuyEFF({ obj }, { put, call, select }) {

            const evaluateList = yield select((state)=>state.evaluateList);
            const result = yield call(addCartOrBuy, {  goodsId:evaluateList.goodsId, count:1, specId:evaluateList.specId , ...obj });
            if (result.code == 200) {
                if(obj.saveType==1){
                    yield put(routerRedux.push('/order'))
                }else {
                    Toast.success(result.msg, 1.5);
                }

            } else {

            }
        },

    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, query }) => {
                if(pathname.includes('/evaluateList/')){
                    const match = pathToRegexp('/evaluateList/:goodsId/:specId').exec(pathname);
                    dispatch({ type:'evaluatesEFF', obj:{ goodsId:match[1],gevalScore:null, pageNo:1, specId:match[2] } });
                }
            });
        },
    },
};
